package com.sk.bilibili.service;

import com.sk.bilibili.bean.Danmu;
import com.sk.bilibili.dto.DanmuInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 邵康
 * @date 2021/12/2 14:15
 */
@Service
public class LiveSocketService extends BaseBroadcastSocketService<DanmuInfo>{

    @Autowired
    private MemberService memberService;

    @Override
    protected void afterAddNew(Integer liveId, Session session) {
        broadcastOnlineChange(liveId);
    }

    private void broadcastOnlineChange(Integer liveId) {
        Set<Session> sessions = sessionMap.get(liveId);
        broadcastMessage(liveId,DanmuInfo.onlineInfo(sessions.size()));
    }

    @Override
    protected void afterRemove(Integer liveId, Session session) {
        broadcastOnlineChange(liveId);
    }

    public void broadcastDanmu(Integer liveId, Danmu danmu) {
        danmu.getExtra().put("nickName",memberService.getNickName(danmu.getMemberId()));
        broadcastMessage(liveId,DanmuInfo.danmuInfo(danmu));
    }

}
